package com.pfzhang.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;

class Bird<T>{
	void m(T t){
		System.out.println(t);
	}
}


public class TestReflection {

	Bird<? extends Integer> b = new Bird<>();

	<V> void m(String s){
		
	}
	
	public static void main(String[] args) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
	
		Class<TestReflection> c = TestReflection.class;
		Field f1 = c.getDeclaredField("b");
		if(f1.getGenericType() instanceof ParameterizedType){
			String name =((ParameterizedType) f1.getGenericType()).getActualTypeArguments()[0].getTypeName();
			System.out.println(name);
			
			if(((ParameterizedType) f1.getGenericType()).getActualTypeArguments()[0] instanceof WildcardType){
				String name1 =((WildcardType)((ParameterizedType) f1.getGenericType()).getActualTypeArguments()[0]).getUpperBounds()[0].getTypeName();
				System.out.println(name1);
			}
		}

		Method m = c.getDeclaredMethod("m",String.class);
	    TypeVariable<Method> t = m.getTypeParameters()[0];
		System.out.println(t.getName());
		System.out.println(m.getReturnType().getName());
		
		System.out.println("----------------------------");
		
		Bird<String> b1 = new Bird<>();
		b1.m("abc");
		Class<?> c2 = b1.getClass();
		Method m2 = c2.getMethod("m", Object.class);
		m2.invoke(b1, 1);
		
	}
	
	

}
